package com.koron.css2.meterService.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.css2.ServerInterface;
import com.koron.css2.baseConfig.bean.MeterFactoryBean;
import com.koron.css2.baseConfig.mapper.BusinessAbodeMapper;
import com.koron.css2.baseConfig.mapper.BusinessAreaMapper;
import com.koron.css2.baseConfig.mapper.MeterFactoryMapper;
import com.koron.css2.baseConfig.vo.SelectVO;
import com.koron.css2.meterService.bean.query.MsInfoPlanQueryBean;
import com.koron.css2.meterService.dto.MsInfoPlanDto;
import com.koron.css2.meterService.mapper.MsInfoPlanMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * 水表入库列表查询
 * @author bin
 *
 */
public class MsStorageList implements ServerInterface{

	@Override
	public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {
		@SuppressWarnings("rawtypes")
		MessageBean<PageInfo> info = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "success", PageInfo.class);
		try {
			MsInfoPlanQueryBean bean = JsonUtils.objectToPojo(req.getData(), MsInfoPlanQueryBean.class);
			
			MsInfoPlanMapper mapper = factory.getMapper(MsInfoPlanMapper.class);	
//			//首先根据条件获取水表型号id
//			MeterModelMapper modelMapper = factory.getMapper(MeterModelMapper.class);
//			List<MeterModelBean> models=modelMapper.queryMeterModelList(new MsInfoQueryBean());
//			//循环遍历获取型号信息
//			Map<String,MeterModelBean> modelMap=new HashMap<String,MeterModelBean>();
//			List<String> modelIds=new ArrayList<String>();
			Map<String, String> meterTypes = Tools.mapDicByCode(factory, "MMT");		//水表类型
			Map<String, String> transmittalModel = Tools.mapDicByCode(factory, "TPW");//传输方式
//			for(MeterModelBean model:models) {
//				model.setMeterTypeName(meterTypes.get(model.getMeterType()));
//				model.setTransWayName(transmittalModel.get(model.getTransWay()));
//				modelMap.put(model.getId(), model);
//				modelIds.add(model.getId());
//			}
			PageHelper.startPage(bean.getPage(), bean.getPageCount());
			List<MsInfoPlanDto> list = mapper.selectMsInfoPlanList(bean);
			Map<String, String> meterForms = Tools.mapDicByCode(factory, "MMF"); //水表形态
			Map<String, String> commOperators = Tools.mapDicByCode(factory, "TXS");//水表通讯商
			Map<String, String> protocols = Tools.mapDicByCode(factory, "XXGY"); //下行规约
			Map<String, String> meterPatterns = Tools.mapDicByCode(factory, "MMP");//水表型式
			Map<String, String> meterSensors = Tools.mapDicByCode(factory, "MSS"); //水表传感器
			//营业所
			BusinessAbodeMapper Businessmapper = factory.getMapper(BusinessAbodeMapper.class,"_default");
			 //水表厂商
            MeterFactoryMapper meterFactoryMapper=factory.getMapper(MeterFactoryMapper.class);

			//营业区域
			List<SelectVO> businessAreaList = factory.getMapper(BusinessAreaMapper.class).select(null, userInfo.getCurWaterCode());
			list.stream().forEach(t -> {
				
//				if(StringUtils.isNotBlank(t.getMeterModel())) {
//					//水表口径
//					t.setMeterBoreName(modelMap.get(t.getMeterModel()).getMeterBoreName());
//					t.setMeterTypeName(modelMap.get(t.getMeterModel()).getMeterTypeName());
//					t.setMeterFactoryName(modelMap.get(t.getMeterModel()).getFactoryName());
//					t.setTransmittalModelName(modelMap.get(t.getMeterModel()).getTransWayName());
//				}
				if(t.getValveControl() != null) {
					if(1 == t.getValveControl())
						t.setValveControlName("是");
					else
						t.setValveControlName("否");
				}
				if(StringUtils.isNotBlank(t.getMeterFactory())) {
					MeterFactoryBean factoryBean = meterFactoryMapper.selectMeterFactoryById(t.getMeterFactory());
					t.setMeterFactoryName(factoryBean == null ?t.getMeterFactory():factoryBean.getFactoryName());
				}
				if(StringUtils.isNotBlank(t.getMeterType())) {
					//水表类型
					t.setMeterTypeName(meterTypes.get(t.getMeterType()));
				}
				if(StringUtils.isNotBlank(t.getProtocol())) {
					//下行规约
					t.setProtocolName(protocols.get(t.getProtocol()));
				}
				if(StringUtils.isNotBlank(t.getMeterPattern())) {
					//水表型式
					t.setMeterPatternName(meterPatterns.get(t.getMeterPattern()));
				}
				if(StringUtils.isNotBlank(t.getMeterSensor())) {
					//水表型式
					t.setMeterSensorName(meterSensors.get(t.getMeterSensor()));
				}
				if(StringUtils.isNotBlank(t.getMeterForm())) {
					//水表形态
					t.setMeterFormName(meterForms.get(t.getMeterForm()));
				}
				if(StringUtils.isNotBlank(t.getCommOperator())) {
					//通讯商
					t.setCommOperatorName(commOperators.get(t.getCommOperator()));
				}
				if(StringUtils.isNotBlank(t.getTransmittalModel())) {
					//传输方式
					t.setTransmittalModelName(transmittalModel.get(t.getTransmittalModel()));
				}
				if(StringUtils.isNotBlank(t.getDepartBelong())) {
					Optional<SelectVO> optional = businessAreaList.stream().filter(o -> Objects.equals(o.getId(), t.getDepartBelong())).findFirst();
					t.setDepartBelongName(optional.orElse(new SelectVO()).getName());
				}
				t.setProcessStateName(Tools.transProcessStatus(t.getProcessState()));
			});
			info.setData(new PageInfo<MsInfoPlanDto>(list));
		} catch (Exception e) {
			info.setCode(Constant.MESSAGE_INT_FAIL);
			info.setDescription(e.getMessage());
			logger.error(e.getMessage(), e);
		}
		return info;
	}

}
